#!/usr/bin/env python

#$Id: copyraw.py 68 2007-08-07 00:55:44Z bouwens $
# -------------------------------------------------------------
#
__version__      = '$Revision: 68 $ '[11:-3]
__version_date__ = '$Date: 2007-08-07 00:55:44 +0000 (Tue, 07 Aug 2007) $ '[7:-3]
__author__       = 'R. Bouwens, <bouwens@ucolick.org>, D. Magee, <magee@ucolick.org>'

import os
import os.path as op
import pyfits

def convertValues(v):
    try:
        return eval(v)
    except:
        return v

def copyRawData(rawlist, updatekeys, rawdir):
    for i in rawlist:
        fimin = op.join(rawdir, i)
        fimout = i.replace('raw.fits', 'raw0.fits')
        print 'Copying %s to %s' % (fimin, fimout)
        fim = pyfits.open(fimin)
        phdr = fim[0].header
        if updatekeys == None:
            fim.writeto(fimout)
        else:
            for j in updatekeys:
                k, v = j
                if (k.upper() != 'DARKFILE') or (phdr['SAMP_SEQ'].startswith('SPARS64')):
                  phdr.update(k, convertValues(v))
            fim.writeto(fimout)
        fim.close()
    for i in rawlist:
        nf = i.replace('raw.fits','spt.fits')
        fimin = op.join(rawdir, nf)
	if os.path.exists(fimin):
          print 'Copying %s to %s' % (fimin, nf)
          fim = pyfits.open(fimin)
	  fim.writeto(nf)
          fim.close()

